package com.xs.mongo.spring;

import java.util.Date;
import java.util.List;

import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;

import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.model.Filters;
import com.mongodb.client.result.DeleteResult;

public class SpringMain {
	private static Logger LOGGER = LoggerFactory.getLogger(SpringMain.class);

	public static void main(String[] args) {
		@SuppressWarnings("resource")
		ApplicationContext app = new AnnotationConfigApplicationContext(MongoConfig.class);


		MongoTemplate mongoTemplate = (MongoTemplate) app.getBean("mongoTemplate");

		User user = new User();
		user.setUserName("AAAAAAAA");
		mongoTemplate.insert(user);

		Query query = new Query();

		List<User> userList = mongoTemplate.find(query, User.class, "user_rongyun");

		userList.stream().forEach(e -> {
			LOGGER.info(e.toString());
		});

	}

	static void basicTest() {
		@SuppressWarnings("resource")
		ApplicationContext app = new AnnotationConfigApplicationContext(MongoConfig.class);
		@SuppressWarnings("unchecked")
		MongoCollection<Document> collection = (MongoCollection<Document>) app.getBean("mongoCollection");

		/*
		 * for (int i = 0; i < 20; i++) { collection.insertOne(new
		 * Document().append("userName", "A" + i).append("age", i) .append("address",
		 * "xi an " + i).append("birthday", new Date())); }
		 */

		DeleteResult deleteOne = collection.deleteOne(Filters.eq("age", 10));

		LOGGER.info(deleteOne.toString());

		FindIterable<Document> find = collection.find(Filters.gt("age", 10)).sort(new Document().append("age", -1));

		MongoCursor<Document> iterator = find.iterator();

		while (iterator.hasNext()) {
			Document next = iterator.next();
			LOGGER.info(next.toJson());
		}
	}

	static void testPage() {
		@SuppressWarnings("resource")
		ApplicationContext app = new AnnotationConfigApplicationContext(MongoConfig.class);
		@SuppressWarnings("unchecked")
		MongoCollection<Document> collection = (MongoCollection<Document>) app.getBean("mongoCollection");

		for (int i = 0; i < 26; i++) {
			collection.insertOne(new Document().append("userName", "A" + i).append("age", i)
					.append("address", "xi an " + i).append("birthday", new Date()));
		}

		FindIterable<Document> findAll = collection.find().sort(new Document().append("age", 1));

		MongoCursor<Document> iteratorAll = findAll.iterator();

		while (iteratorAll.hasNext()) {
			Document next = iteratorAll.next();
			LOGGER.info(next.toJson());
		}

		long totalSize = collection.countDocuments();// 总条数

		int pageSize = 10;

		int pages = (int) (totalSize % pageSize == 0 ? totalSize / pageSize : (totalSize / pageSize) + 1);// 页数

		// 第一页

		int pageNum = 3;

		FindIterable<Document> find = collection.find().skip((int) ((pageNum - 1) * pageSize)).limit(pageSize)
				.sort(new Document().append("age", 1));

		MongoCursor<Document> iterator = find.iterator();

		while (iterator.hasNext()) {
			Document next = iterator.next();
			LOGGER.info(next.toJson());
		}
	}

}
